package com.ruoyi.web.controller.tool;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import cn.hutool.json.JSONUtil;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.ruoyi.bigScreen.vo.KeyValueVO;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.zzw.*;
import com.ruoyi.common.core.domain.zzw.BO.DeptBO;
import com.ruoyi.common.core.domain.zzw.VO.NewRenderingData;
import com.ruoyi.common.core.domain.zzw.VO.PointData;
import com.ruoyi.common.core.domain.zzw.VO.PointDataVO;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.mapper.TIndicatorsInfoMapper;
import com.ruoyi.system.mapper.TPeopleManageMapper;
import com.ruoyi.system.mapper.TRegionInfoMapper;
import com.ruoyi.system.mapper.TRescueStrengthMapper;
import com.ruoyi.system.service.ITIndicatorsInfoService;
import com.ruoyi.system.service.ITRegionInfoService;
import com.ruoyi.system.service.ITSafenessIndexService;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;

/**
 * swagger 用户测试方法
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/screen/test")
public class TestController extends BaseController {

    @Autowired
    private ITIndicatorsInfoService indicatorsInfoService;
    @Autowired
    private ITRegionInfoService regionInfoService;
    @Autowired
    private ITSafenessIndexService safenessIndexService;
    @Autowired
    private TRegionInfoMapper tRegionInfoMapper;
    @Autowired
    private TIndicatorsInfoMapper tIndicatorsInfoMapper;
    @Autowired
    private TRescueStrengthMapper rescueStrengthMapper;
    @Autowired
    private TPeopleManageMapper tPeopleManageMapper;
    @GetMapping(value = "/setIndexIndicatorsJson")
    public AjaxResult setIndexIndicatorsJson(Long startId, Integer level) {

        String[] in = {
                "非法宗教邪教",
                "暴恐违法犯罪",
                "进京上访",
                "治安案件发案率",
                "刑事案件发案率",
                "危险品事件发案率",
                "高危人员事件发案率",
                "特定利益群体聚众事件发案率",
                "聚众破坏社会秩序事件发案率",
                "涉黑涉恶犯罪",
                "群体性劳动纠纷事件发案率",
                "城镇房地产纠纷事件发案率",
                "农村土地林地纠纷事件发案率",
                "新型网络电信诈骗、赌博事件发案率",
                "市场经济犯罪事件发案率",
                "金融运行犯罪事件发案率",
                "道路交通安全事件发案率",
                "工程与生产安全事件发案率",
                "校园安全事件发案率",
                "公共卫生安全事件发案率",
                "消防安全事件发案率",
                "食药品安全事件发案率",
                "网络舆情事件发案率",
                "网络安全事件发案率",
                "信息安全事件发案率",
                "群众服务满意度"
        };

        List<TRegionInfo> regionInfoList = regionInfoService.list();
        Map<Long, String> reMap = regionInfoList.stream().collect(Collectors.toMap(TRegionInfo::getCRegionId, TRegionInfo::getCRegionName));

        QueryWrapper<TSafenessIndex> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TSafenessIndex::getcLevel, level).ge(TSafenessIndex::getcSafenessId, startId);
        List<TSafenessIndex> list = safenessIndexService.list(queryWrapper);
        for (TSafenessIndex index : list) {
            write(index, reMap, level);
        }
        return AjaxResult.success();
    }


    private void write(TSafenessIndex index, Map<Long, String> reMap, int level) {
        String disName = reMap.getOrDefault(index.getcRegionId(), "");
        if (1 == level) {
            String type = "1";
            String tab = "1";
            if ("社会治安".equals(index.getcSafenessName())) {
                type = "2";
                tab = "1";
            }
            if ("公共安全".equals(index.getcSafenessName())) {
                type = "3";
                tab = "3";
            }
            if ("司法案件".equals(index.getcSafenessName())) {
                type = "1";
                tab = "2";
            }
            if ("网络安全".equals(index.getcSafenessName())) {
                type = "2";
                tab = "2";
            }
            if ("人民群众满意度".equals(index.getcSafenessName())) {
                type = "3";
                tab = "4";
            }
            createLevelOne(index.getcSafenessName(), type, tab, index.getcRegionId().toString(), disName, index.getcSafenessId());
        }

        if (2 == level) {
            create1(index.getcSafenessName(), "2", "3", index.getcRegionId().toString(), disName, index.getcSafenessId());
            create2(index.getcSafenessName(), "2", "4", index.getcRegionId().toString(), disName, index.getcSafenessId());
            create3(index.getcSafenessName(), "4", "1", index.getcRegionId().toString(), disName, index.getcSafenessId());
            create4(index.getcSafenessName(), "4", "2", index.getcRegionId().toString(), disName, index.getcSafenessId());
        }

    }

    private void createLevelOne(String name, String type, String tab, String districtId, String districtName, Long indexId) {
        List<Integer> values = getValues(12);
        String json = String.format(
                "[{\"name\":\"一月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"二月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"三月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"四月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"五月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"六月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"七月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"八月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"九月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"十月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"十一月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"十二月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}]",
                values.get(0),
                values.get(1),
                values.get(2),
                values.get(3),
                values.get(4),
                values.get(5),
                values.get(6),
                values.get(7),
                values.get(8),
                values.get(9),
                values.get(10),
                values.get(11)
        );

        saveIndicator(type, tab, districtId, districtName, indexId, name, json, 0);

    }

    private void create1(String name, String type, String tab, String districtId, String districtName, Long indexId) {
        String title = name + "当前指数";
        List<Integer> values = getValues(12);
        String json = String.format(
                "[{\"name\":\"一月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"二月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"三月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"四月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"五月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"六月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}]",
                values.get(0),
                values.get(1),
                values.get(2),
                values.get(3),
                values.get(4),
                values.get(5)
        );

        saveIndicator(type, tab, districtId, districtName, indexId, title, json, 1);

    }


    private void create2(String name, String type, String tab, String districtId, String districtName, Long indexId) {
        String title = name + "预警指数";
        List<Integer> values = getValues(6);
        String json = String.format(
                "[{\"name\":\"七月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"八月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"九月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"十月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"十一月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"十二月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}]",
                values.get(0),
                values.get(1),
                values.get(2),
                values.get(3),
                values.get(4),
                values.get(5)
        );

        saveIndicator(type, tab, districtId, districtName, indexId, title, json, 1);

    }

    private void create3(String name, String type, String tab, String districtId, String districtName, Long indexId) {
        String title = name + "预警地点";
        List<TRegionInfo> regionList = regionInfoService.listByParent(districtId);
        List<String> xNames = regionList.stream().map(TRegionInfo::getCRegionName).collect(Collectors.toList());
        List<JSONObject> itemList = getItemList(xNames, new ArrayList<>());
        String json = JSON.toJSONString(itemList);

        saveIndicator(type, tab, districtId, districtName, indexId, title, json, 1);

    }

    private void create4(String name, String type, String tab, String districtId, String districtName, Long indexId) {
        String title = name + "历史预警记录";
        List<Integer> values = getRateValues(6);
        String json = String.format(
                "[{\"name\":\"案例一\",\"value\":\"%d\"}\n" +
                        ",{\"name\":\"案例二\",\"value\":\"%d\"}\n" +
                        ",{\"name\":\"案例三\",\"value\":\"%d\"}\n" +
                        ",{\"name\":\"案例四\",\"value\":\"%d\"}\n" +
                        ",{\"name\":\"案例五\",\"value\":\"%d\"}\n" +
                        ",{\"name\":\"案例六\",\"value\":\"%d\"}]",
                values.get(0),
                values.get(1),
                values.get(2),
                values.get(3),
                values.get(4),
                values.get(5)
        );

        saveIndicator(type, tab, districtId, districtName, indexId, title, json, 1);

    }

    private void saveIndicator(String type, String tab, String districtId, String districtName, Long indexId, String title, String json, int site) {
        TIndicatorsInfo info = new TIndicatorsInfo();
        info.setCIndicatorsName(title);
        info.setCIndicatorsDescribe(title);
        info.setCIndicatorsData(json);
        info.setCIndicatorsType(type);
        info.setCIndicatorsTab(tab);
        info.setCDistrictId(districtId);
        info.setCDistrictName(districtName);
        info.setCOrderNo(1L);
        info.setCSafenessId(indexId);
        info.setCModuleId("8");
        info.setCIndicatorsSite(site);
//        info.setCRelatedKeyEventId();
//        info.setCIsRendering();
//        info.setCRenderingData();
        indicatorsInfoService.save(info);
    }


    /**
     * 生成指定区域的指定指标数据，没有指定指标则默认为福建省下的所有指标
     * 未指定X轴默认为要生成区域下的下级行政区
     * 要分组并且未指定分组默认为 [2021,2022,2023]
     *
     * @param indicatorIds   要复制的指标
     * @param indicatorNames 要新生成的指标名称
     * @param xNames         X轴
     * @param group          是否分组
     * @param groupNames     分组名称
     * @param districtId     地区ID
     * @param districtName   地区名称
     * @return
     */
    @PostMapping(value = "/setIndicatorsJson")
    public AjaxResult setIndicatorsJson(String districtId, String districtName) {
        List<String> indicatorIds = new ArrayList<>();
        List<String> indicatorNames = new ArrayList<>();
        List<String> xNames = new ArrayList<>();
        List<String> groupNames = new ArrayList<>();
        Boolean group = true;
        if (group && CollectionUtils.isEmpty(groupNames)) {
            groupNames = Arrays.asList("2021", "2022", "2023");
        }
        if (CollectionUtils.isEmpty(xNames)) {
            List<TRegionInfo> regionList = regionInfoService.listByParent(districtId);
            xNames = regionList.stream().map(TRegionInfo::getCRegionName).collect(Collectors.toList());
        }
        generateByCopy(indicatorIds, xNames, groupNames, districtId, districtName);
//        if (CollectionUtils.isNotEmpty(indicatorIds)) {
//            generateByCopy(indicatorIds, xNames, groupNames, districtId, districtName);
//        }
//        if (CollectionUtils.isNotEmpty(indicatorNames)) {
//            generateByAdd(indicatorNames, xNames, groupNames, districtId, districtName);
//        }
        return AjaxResult.success();
    }

    private void generateByCopy(List<String> indicatorIds, List<String> xNames, List<String> groupNames, String districtId, String districtName) {
        List<TIndicatorsInfo> list;
        if (CollectionUtils.isNotEmpty(indicatorIds)) {
            list = indicatorsInfoService.listByIds(indicatorIds);
        } else {
            QueryWrapper<TIndicatorsInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(TIndicatorsInfo::getCDistrictId, "5").isNull(TIndicatorsInfo::getCSafenessId);
            list = indicatorsInfoService.list(queryWrapper);
        }
        for (TIndicatorsInfo info : list) {
            if ("湖北省".equals(info.getCDistrictName())) {
                TIndicatorsInfo add = new TIndicatorsInfo();
                BeanUtils.copyProperties(info, add);
                add.setCIndicatorsId(null);

                generateData(xNames, groupNames, districtId, districtName, add);
            }
        }
    }

    @PostMapping(value = "/addIndicatorsJson")
    public AjaxResult addIndicatorsJson(String districtId, String districtName) {

        List<String>  indicatorNames =Arrays.asList("风险趋势分析");
        List<String>  xNames =Arrays.asList("2023-06","2023-07","2023-08","2023-09","2023-10","2023-11");
        List<String>  groupNames =Arrays.asList("高","中","低");
        generateByAdd(indicatorNames, xNames, groupNames, districtId, districtName);
        return success();
    }

    private void generateByAdd(List<String> indicatorNames, List<String> xNames, List<String> groupNames, String districtId, String districtName) {

        for (String name : indicatorNames) {
            TIndicatorsInfo add = new TIndicatorsInfo();
            add.setCIndicatorsName(name);
            add.setCIndicatorsDescribe(name);
            add.setCDistrictId(districtId);
            add.setCDistrictName(districtName);
            add.setCDrillDownData(null);
            List<JSONObject> itemList = getItemList(xNames, groupNames);
            String json = JSON.toJSONString(itemList);
            add.setCIndicatorsData(json);
            indicatorsInfoService.save(add);
//            generateData(xNames, groupNames, districtId, districtName, add);
        }
    }

    private void generateData(List<String> xNames, List<String> groupNames, String districtId, String districtName, TIndicatorsInfo add) {
        add.setCDistrictId(districtId);
        add.setCDistrictName(districtName);
        add.setCDrillDownData(null);

        List<JSONObject> itemList = new ArrayList<>();
        JSONArray array = JSON.parseArray(add.getCIndicatorsData());
        com.alibaba.fastjson2.JSONObject obj = array.getJSONObject(0);
        String name = obj.getString("name");
        String groupName = obj.getString("group_name");
        if ("武汉市".equals(name)) {
            if ("2021".equals(groupName)) {
                itemList = getItemList(xNames, groupNames);
            } else {
                //
                Set<String> set = new HashSet<>();
                for (int i = 0; i < array.size(); i++) {
                    JSONObject jo = array.getJSONObject(i);
                    String gn = jo.getString("group_name");
                    if (StringUtils.isNotBlank(gn)) {
                        set.add(gn);
                    }
                }
                itemList = getItemList(xNames, new ArrayList<>(set));
            }
        } else {
            Set<String> setn = new HashSet<>();
            for (int i = 0; i < array.size(); i++) {
                JSONObject jo = array.getJSONObject(i);
                String gn = jo.getString("name");
                setn.add(gn);
            }
            itemList = getItemList(new ArrayList<>(setn), groupNames);
        }

        String json = JSON.toJSONString(itemList);
        add.setCIndicatorsData(json);
        if (add.getCIsRendering() != null && add.getCIsRendering() == 1) {
            add.setCRenderingData(json);
        }
        indicatorsInfoService.save(add);
    }

    private List<JSONObject> getItemList(List<String> xNames, List<String> groupNames) {
        List<Integer> values = getValues(xNames.size());
        List<JSONObject> itemList = new ArrayList<>();
        if (!groupNames.isEmpty()) {
            for (String groupName : groupNames) {
                for (int i = 0; i < xNames.size(); i++) {

                    JSONObject item = new JSONObject();
                    item.put("name", xNames.get(i));
                    item.put("value", values.get(i).toString());
                    item.put("max", "");
                    item.put("group_name", groupName);
                    itemList.add(item);
                }
            }
        } else {
            for (int i = 0; i < xNames.size(); i++) {

                JSONObject item = new JSONObject();
                item.put("name", xNames.get(i));
                item.put("value", values.get(i).toString());
                itemList.add(item);
            }
        }
        return itemList;
    }

    private List<Integer> getValues(int len) {
        Random random = new Random();
        List<Integer> values = new ArrayList<>();
        for (int i = 0; i < len; i++) {
            int i1 = random.nextInt(900) + 100;
            values.add(i1);
        }
        return values;
    }

    private List<Integer> getRateValues(int len) {
        Random random = new Random();
        List<Integer> values = new ArrayList<>();
        for (int i = 0; i < len; i++) {
            int i1 = random.nextInt(95);
            values.add(i1);
        }
        return values;
    }

    @GetMapping("/UpdateCRenderingData")
    public AjaxResult UpdateCRenderingData() {
        List<TRegionInfo> tRegionInfos = tRegionInfoMapper.selectTRegionInfoList(null);
        Map<String, String> map = new HashMap<>();
        Map<String, String> longitude = new HashMap<>();
        Map<String, String> latitude = new HashMap<>();
        for (TRegionInfo tRegionInfo : tRegionInfos) {
            map.put(tRegionInfo.getCRegionName(), tRegionInfo.getCAreaCode());
            longitude.put(tRegionInfo.getCRegionName(), tRegionInfo.getCLongitude());
            latitude.put(tRegionInfo.getCRegionName(), tRegionInfo.getCLatitude());
        }
        QueryWrapper<TIndicatorsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TIndicatorsInfo::getCIsRendering, 1);
        List<TIndicatorsInfo> tIndicatorsInfos = indicatorsInfoService.list(queryWrapper);
        List<TIndicatorsInfo> infos = new ArrayList<>();
        for (TIndicatorsInfo tIndicatorsInfo : tIndicatorsInfos) {
            TIndicatorsInfo indicatorsInfo = new TIndicatorsInfo();
            String data = tIndicatorsInfo.getCIndicatorsData();
            List<RenderingData> renderingData = JSON.parseArray(data, RenderingData.class);
            List<NewRenderingData> lists = new ArrayList<>();
            for (RenderingData bean : renderingData) {
                String adcode = map.get(bean.getName());
                if (adcode == null) {
                    continue;
                }
                List<String> value = new ArrayList<>();
                value.add(bean.getValue());
                value.add(longitude.get(bean.getName()));
                value.add(latitude.get(bean.getName()));
                NewRenderingData newRenderingData = new NewRenderingData();
                newRenderingData.setAdcode(adcode);
                newRenderingData.setName(bean.getName());
                newRenderingData.setLevel(getLevel(adcode));
                newRenderingData.setValue(value);
                newRenderingData.setSelected(true);
                newRenderingData.setChildrenNum("");
                newRenderingData.setSubFeatureIndex("16");
                lists.add(newRenderingData);
            }
            BeanUtils.copyProperties(tIndicatorsInfo, indicatorsInfo);
            if (CollectionUtils.isEmpty(lists)) {
                indicatorsInfo.setCIsRendering(0L);
                indicatorsInfo.setCRenderingData("");
            } else {
                String string = JSONUtil.parseArray(lists).toString();
                indicatorsInfo.setCRenderingData(string);
            }
            List<NewRenderingData> collect = lists.stream().collect(Collectors.toMap(NewRenderingData::getName, user -> user, (existing, replacement) -> existing))
                    .values().stream().collect(Collectors.toList());

            String string = JSONUtil.parseArray(collect).toString();
            BeanUtils.copyProperties(tIndicatorsInfo, indicatorsInfo);
            indicatorsInfo.setCRenderingData(string);
            infos.add(indicatorsInfo);
        }
        for (TIndicatorsInfo info : infos) {
            tIndicatorsInfoMapper.updateRenderingData(info);
        }
        return success(infos);
    }

    private String getLevel(String adcode) {
        if (adcode.endsWith("0000")) {
            return "province";
        }
        if (adcode.endsWith("00")) {
            return "city";
        }
        return "district";
    }

    @GetMapping("/addSafeness")
    public AjaxResult addSafeness(TSafenessIndex safenessIndex) {
        List<TSafenessIndex> indices = new ArrayList<>();
        String[] zzaq = {"非法宗教邪教", "暴恐违法犯罪", "进京上访"};
        String[] shza = {"治安案件发案率", "刑事案件发案率", "危险品事件发案率", "高危人员事件发案率", "特定利益群体聚众事件发案率", "聚众破坏社会秩序事件发案率", "涉黑涉恶犯罪"};
        String[] sfaj = {"群体性劳动纠纷事件发案率", "城镇房地产纠纷事件发案率", "农村土地林地纠纷事件发案率", "新型网络电信诈骗、赌博事件发案率", "市场经济犯罪事件发案率", "金融运行犯罪事件发案率"};
        String[] ggaq = {"道路交通安全事件发案率", "工程与生产安全事件发案率", "校园安全事件发案率", "公共卫生安全事件发案率", "消防安全事件发案率", "食药品安全事件发案率"};
        String[] wlan = {"网络舆情事件发案率", "网络安全事件发案率", "信息安全事件发案率"};
        String[] rmqzmyd = {"群众服务满意度"};
        String[] fist = {"政治安全", "社会治安", "司法案件", "公共安全", "网络安全", "人民群众满意度"};
        Map<String, String[]> map = new HashMap<>();
        map.put("政治安全", zzaq);
        map.put("社会治安", shza);
        map.put("公共安全", ggaq);
        map.put("司法案件", sfaj);
        map.put("网络安全", wlan);
        map.put("人民群众满意度", rmqzmyd);
        String[] month = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
        Long[] regionId = {23l};
        for (Long region : regionId) {
            for (String s : month) {
                for (String name : fist) {
                    Random random = new Random();
                    TSafenessIndex index = new TSafenessIndex();
                    int id = 10000000 + random.nextInt(90000000);
                    index.setcSafenessId((long) id);
                    index.setcSafenessName(name);
                    int randomNumber = random.nextInt(90) + 10;
                    index.setcSafenessIndex(String.valueOf(randomNumber));
                    index.setcRegionId(region);
                    index.setcIsDelete(1l);
                    index.setcYear("2023");
                    index.setcMonth(s);
                    index.setcPid(null);
                    index.setcLevel("1");
                    index.setcStatus((long) (random.nextInt(3) + 1));
                    String[] strings = map.get(name);
                    indices.add(index);
                    for (String string : strings) {
                        TSafenessIndex index2 = new TSafenessIndex();
                        int id2 = 10000000 + random.nextInt(90000000);
                        index2.setcSafenessId((long) id2);
                        index2.setcSafenessName(string);
                        int randomNumber2 = random.nextInt(90) + 10;
                        index2.setcSafenessIndex(String.valueOf(randomNumber2));
                        index2.setcRegionId(region);
                        index2.setcIsDelete(1l);
                        index2.setcYear("2022");
                        index2.setcMonth(s);
                        index2.setcPid((long) id);
                        index2.setcLevel("2");
                        index2.setcStatus((long) (random.nextInt(3) + 1));
                        indices.add(index2);
                    }
                }
            }
        }


        for (TSafenessIndex index : indices) {
            safenessIndexService.insertTSafenessIndex(index);
        }
        return success(indices);
    }

    /**
     * 社会治理-风险治理
     *
     * @param parentId
     * @return
     */
    @GetMapping("/addFXZL")
    public AjaxResult addFXZL(Long parentId) {
        List<TRegionInfo> tRegionInfos = tRegionInfoMapper.selectTRegionInfo(parentId);

        String[] type = {"人员数量", "威胁程度", "在逃状态", "地域分布", "打击成果", "人员类型", "人员特征",};
        String json2 = getJson(type);
        String c_rendering_data2 = "[" + json2.substring(0, json2.length() - 1) + "]";

        Long order = 0l;
        List<TIndicatorsInfo> infos = new ArrayList<>();
        String[] title = {"安全隐患态势分析", "各省交通安全分析", "各省消防安全分析", "各省食品安全分析", "各省安全生产案件量分析", "各省药品安全分析", "各省治安态势分析", "重点人员管控"};
        for (String s : title) {
            TIndicatorsInfo info = new TIndicatorsInfo();
            if (s.equals("重点人员管控")) {
                info.setCIndicatorsData(c_rendering_data2);
                info.setCRenderingData(c_rendering_data2);
                info.setCOrderNo(12l);
                info.setCIndicatorsType("6");
                info.setCIndicatorsTab("1");
            } else {
                String json1 = getNoGroup(tRegionInfos);
                info.setCIndicatorsData(json1);
                info.setCRenderingData(json1);
                info.setCOrderNo(order++);
                info.setCIndicatorsType("1");
                info.setCIndicatorsTab("1");
            }
            info.setCIndicatorsName(s);
            info.setCIndicatorsDescribe(s);
            info.setCDistrictId(String.valueOf(parentId));
            info.setCModuleId("16");
            info.setCIsRendering(1l);
            if (order > 5) {
                info.setCIndicatorsSite(1);
            } else {
                info.setCIndicatorsSite(0);
            }
            info.setCIsDelete(1);
            infos.add(info);
        }
        install(infos);
        return success(infos);
    }

    private void install(List<TIndicatorsInfo> infos) {
        for (TIndicatorsInfo info : infos) {
            tIndicatorsInfoMapper.insert(info);
        }
    }

    /**
     * 社会治理-矛盾纠纷
     *
     * @param parentId
     * @return
     */

    @GetMapping("/addMDJF")
    public AjaxResult addMDJF(Long parentId) {
        List<TRegionInfo> tRegionInfos = tRegionInfoMapper.selectTRegionInfo(parentId);

        String[] san = {"征地拆迁", "村矿矛盾", "医患关系", "经济纠纷", "民转刑事案件", "涉及群众利益"};
        String[] one = {"兼职调解员", "基层调解组织", "行业调解员", "专职调解员", "基层调解组织"};
        String[] si = {"2021", "2022", "2023"};
        List<TIndicatorsInfo> infos = new ArrayList<>();

        Long order = 0L;
        //基层调解力量分析
        String json1 = getGroup(tRegionInfos, one);
        getaLong(parentId, "基层调解力量分析", json1, order, infos, "17");
        //矛盾纠纷趋势研判
        String json2 = getNoGroup(tRegionInfos);
        getaLong(parentId, "矛盾纠纷趋势研判", json2, order, infos, "17");
        //高危矛盾预警预测
        String json3 = getGroup(tRegionInfos, san);
        getaLong(parentId, "高危矛盾预警预测", json3, order, infos, "17");
        //调解案件量统计
        String json4 = getGroup(tRegionInfos, si);
        getaLong(parentId, "调解案件量统计", json4, order, infos, "17");
        //诉讼案件量统计
        String json5 = getNoGroup(tRegionInfos);
        getaLong(parentId, "诉讼案件量统计", json5, order, infos, "17");
        //疑难案件量统计
        String json6 = getNoGroup(tRegionInfos);
        getaLong(parentId, "疑难案件量统计", json6, order, infos, "17");
        //征地拆迁案件量统计
        String json7 = getNoGroup(tRegionInfos);
        getaLong(parentId, "征地拆迁案件量统计", json7, order, infos, "17");
        //村矿矛盾案件量统计
        String json8 = getNoGroup(tRegionInfos);
        getaLong(parentId, "村矿矛盾案件量统计", json8, order, infos, "17");
        //医患关系案件量统计
        String json9 = getNoGroup(tRegionInfos);
        getaLong(parentId, "医患关系案件量统计", json9, order, infos, "17");
        //特殊利益群体矛盾纠纷案件量统计
        String json10 = getNoGroup(tRegionInfos);
        getaLong(parentId, "特殊利益群体矛盾纠纷案件量统计", json10, order, infos, "17");
        //经济纠纷案件量统计
        String json11 = getNoGroup(tRegionInfos);
        getaLong(parentId, "经济纠纷案件量统计", json11, order, infos, "17");
        //民转刑案件量统计
        String json12 = getNoGroup(tRegionInfos);
        getaLong(parentId, "民转刑案件量统计", json12, order, infos, "17");
        install(infos);
        return success(infos);
    }

    @GetMapping("/addSQMY")
    public AjaxResult addSQMY(Long parentId) {
        List<TRegionInfo> tRegionInfos = tRegionInfoMapper.selectTRegionInfo(parentId);
        List<TIndicatorsInfo> infos = new ArrayList<>();
        Long order = 0L;
        String[] si = {"2021", "2022", "2023"};
        String[] sqlb = {"政策相关", "环境相关", "治安相关", "交通相关"};
        String[] rdht = {"AI", "绿色发展和环保", "经济发展和产业升级", "全球卫生安全", "教育改革", "缅北", "国际关系"};
        //热点话题分析
        String json1 = getJson(rdht);
        getaLong(parentId, "热点话题分析", json1, 1l, infos, "18");
        //区域诉求量分析
        String json2 = getGroup(tRegionInfos, si);
        getaLong(parentId, "区域诉求量分析", json2, 2l, infos, "18");
        //话题感知指数分析
        String json3 = getNoGroup(tRegionInfos);
        getaLong(parentId, "话题感知指数分析", json3, 3l, infos, "18");
        //诉求列表量分析
        String json4 = getNoGroup(tRegionInfos);
        getaLong(parentId, "诉求列表量分析", json4, 4l, infos, "18");
        //诉求工单量分析
        String json5 = getNoGroup(tRegionInfos);
        getaLong(parentId, "诉求工单量分析", json5, 5l, infos, "18");
        //热点预警分析
        String json6 = getNoGroup(tRegionInfos);
        getaLong(parentId, "热点预警分析", json6, 6l, infos, "18");
        //话题知识图库
        String json7 = getNoGroup(tRegionInfos);
        getaLong(parentId, "话题知识图库", json7, 7l, infos, "18");
        //诉求话题预警分析
        String json8 = getNoGroup(tRegionInfos);
        getaLong(parentId, "诉求话题预警分析", json8, 8l, infos, "18");
        //话题总量统计
        String json9 = getNoGroup(tRegionInfos);
        getaLong(parentId, "话题总量统计", json9, 9l, infos, "18");
        //预警话题数量统计
        String json10 = getNoGroup(tRegionInfos);
        getaLong(parentId, "预警话题数量统计", json10, 10l, infos, "18");
        //诉求总量统计
        String json11 = getNoGroup(tRegionInfos);
        getaLong(parentId, "诉求总量统计", json11, 11l, infos, "18");
        //诉求类别分析
        String json12 = getJson(sqlb);
        getaLong(parentId, "诉求类别分析", json12, 12l, infos, "18");
        install(infos);
        return success(infos);
    }


    //装填实体类数据;
    private static void getaLong(Long parentId, String s, String json, Long order, List<TIndicatorsInfo> infos, String moduleId) {
        TIndicatorsInfo info = new TIndicatorsInfo();
        info.setCIndicatorsData(json);
        info.setCRenderingData(json);
        info.setCOrderNo(order++);
        info.setCIndicatorsType("1");
        info.setCIndicatorsTab("1");
        info.setCIndicatorsName(s);
        info.setCIndicatorsDescribe(s);
        info.setCDistrictId(String.valueOf(parentId));
        info.setCModuleId(moduleId);
        info.setCIsRendering(1l);
        if (order > 5) {
            info.setCIndicatorsSite(1);
        } else {
            info.setCIndicatorsSite(0);
        }
        info.setCIsDelete(1);
        infos.add(info);
    }

    //有分组的json数据;
    private static String getGroup(List<TRegionInfo> tRegionInfos, String[] one) {
        String json1 = new String();
        for (TRegionInfo tRegionInfo : tRegionInfos) {
            Random random = new Random();
            for (String s : one) {
                int sum = random.nextInt(900) + 100;
                json1 = String.format(
                        json1 + "{\"name\":\"%s\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"%s\"},", tRegionInfo.getCRegionName(), sum, s
                );
            }
        }
        json1 = "[" + json1.substring(0, json1.length() - 1) + "]";
        return json1;
    }

    //有分组且不为地区的json数据;
    private static String getGroup2(String[] xs, String[] one) {
        String json1 = new String();
        for (String x : xs) {
            Random random = new Random();
            for (String s : one) {
                int sum = random.nextInt(900) + 100;
                json1 = String.format(
                        json1 + "{\"name\":\"%s\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"%s\"},", x, sum, s
                );
            }
        }
        json1 = "[" + json1.substring(0, json1.length() - 1) + "]";
        return json1;
    }

    private static String getJson(String[] type) {
        String json2 = new String();
        for (String s : type) {
            Random random = new Random();
            int sum = 100000 + random.nextInt(900000);
            json2 = String.format(
                    json2 + "{\"name\":\"%s\",\"value\":\"%d\"},", s, sum
            );
        }
        json2 = "[" + json2.substring(0, json2.length() - 1) + "]";
        return json2;
    }

    //没有分组的json数据;
    private static String getNoGroup(List<TRegionInfo> tRegionInfos) {
        String json1 = new String();
        for (TRegionInfo tRegionInfo : tRegionInfos) {
            Random random = new Random();
            int sum = random.nextInt(900) + 100;
            json1 = String.format(
                    json1 + "{\"name\":\"%s\",\"value\":\"%d\"},", tRegionInfo.getCRegionName(), sum
            );
        }
        json1 = "[" + json1.substring(0, json1.length() - 1) + "]";
        return json1;
    }

    /**
     * 新增部门
     */
    @GetMapping("/addDept")
    public AjaxResult addDept(){
        String[] depts={"应急管理局","宣传部","民政局","公安局","环境局","区委办", "财政局","人社局","住建局","交通局",
                "卫生局","街道办","急管理局","急管理局","最高检","最高法","司法部","国安部"};

        Map<String,String> deptType=new HashMap<>();
        Integer type=1;
        for (String dept : depts) {
            deptType.put(dept, String.valueOf((type++)));
        }
        List<TDepartmentManage> list=new ArrayList<>();
        List<TRegionInfo> tRegionInfos = tRegionInfoMapper.selectTRegionInfo(1l);
        for (String dept : depts) {
            Random random = new Random();
            int deptId = 10000000 + random.nextInt(90000000);
            TDepartmentManage department=new TDepartmentManage();
            department.setcDepartmentId((long) deptId);
            department.setcDepartmentName(dept);
            department.setcDepartmentType(deptType.get(dept));
            department.setcDepartmentDescribe(dept);
            department.setCPid("0");
            list.add(department);
            extracted(dept, tRegionInfos, random, deptId, deptType, list);
        }
        for (TDepartmentManage departmentManage : list) {
            rescueStrengthMapper.install(departmentManage);
        }
        return success();
    }
    //查找所有的子级城市
    private  void extracted(String dept, List<TRegionInfo> tRegionInfos, Random random, int deptId, Map<String, String> deptType, List<TDepartmentManage> list) {
        for (TRegionInfo tRegionInfo : tRegionInfos) {
                TDepartmentManage department2=new TDepartmentManage();
                int deptId2 = 10000000 + random.nextInt(90000000);
                department2.setcDepartmentId((long) deptId2);
                department2.setcDepartmentName(tRegionInfo.getCRegionName()+ dept);
                department2.setCPid(String.valueOf(deptId));
                department2.setcDepartmentType(deptType.get(dept));
                department2.setcDepartmentDescribe(tRegionInfo.getCRegionName()+ dept);
                list.add(department2);
                List<TRegionInfo> tRegionInfos1 = tRegionInfoMapper.selectTRegionInfo(tRegionInfo.getCRegionId());
                if (CollectionUtils.isNotEmpty(tRegionInfos1)){
                    this.extracted(dept,tRegionInfos1,random, deptId2,deptType,list);
                }
        }
    }

//    @GetMapping("/addPeople")
//    public void addPeople(){
//        List<TDepartmentManage> dept = rescueStrengthMapper.getDeptByType(new DeptBO());
//        List<TPeopleManage> list=new ArrayList<>();
//        for (TDepartmentManage departmentManage : dept) {
//            for (int i = 0; i <3; i++) {
//                //添加带班领导
//                getLeader(departmentManage, "0", 1L, 0L,list);
//                //添加值班员工
//                getLeader(departmentManage, "1", 0L, 1L,list);
//            }
//        }
//        rescueStrengthMapper.insertTPeopleManageList(list);
//    }

    //批量创建人员对象
    private static void getLeader(TDepartmentManage departmentManage, String position, Long leader, Long duty,List<TPeopleManage> list) {
        TPeopleManage tPeople=new TPeopleManage();
        tPeople.setCPeopleId(null);
        tPeople.setCPeopleName(generateChineseName());
        tPeople.setCDepartmentId(String.valueOf(departmentManage.getcDepartmentId()));
        tPeople.setCPosition(position);
        tPeople.setCShiftLeader(leader);
        tPeople.setCOnDuty(duty);
        list.add(tPeople);
    }


    // 随机选择姓氏和名字
    private static String generateChineseName() {
        String[] VOWELS = {"赵", "钱", "孙", "李", "周","吴", "郑", "王", "冯", "陈","赵", "褚", "卫", "蒋", "沈","韩", "杨", "朱", "秦", "尤","许"};
        String[] CONSONANTS = {"翔宇","雨婷","文杰","梦婷","伟强","丽娟","建国","欣儿","博文",
                "佳琪","宇航","丽华","健男","婧宇","文静","浩宇","静雯","俊杰","丹丹","瑞峰","梦婷","明杰","丽丽",
                "峰峰","欢欢","昊然","秀英","文娟","晓敏","静","宇","婷","丽","强","宇","丽"};
        Random random = new Random();

        String surname = VOWELS[random.nextInt(VOWELS.length)];
        String givenName = CONSONANTS[random.nextInt(CONSONANTS.length)];
        // 拼接生成的中文名
        return surname + givenName;
    }

    @GetMapping("/addZYGLPT")
    public void addZYGLPT(){
        String[] two={"今日接入量","历史接入量"};
        String[] si={"数据记录","音频记录","视频记录","结构化数据","表","字段"};
        String[] wu={"公安局系统","法院系统","执法局系统","消防大队系统","派出所系统","住建局系统","城管局系统"};
        List<TRegionInfo> tRegionInfos = tRegionInfoMapper.selectTRegionInfo(1l);
        //省级数据接入情况分析
        String json1 = getGroup(tRegionInfos,two);
        List<TIndicatorsInfo> infos = new ArrayList<>();
        getaLong(1l, "省级数据接入情况分析", json1, 1l, infos, "28");
        //市级数据接入情况分析
        List<TRegionInfo> tRegionInfos2 = tRegionInfoMapper.selectTRegionInfo(11l);
        String json2=getGroup(tRegionInfos2,two);
        getaLong(11l,"市级数据接入情况分析",json2,2l,infos,"28");
        //各系统接入数据表情况分析
        String json3 = getGroup2(wu, two);
        getaLong(1l,"各系统接入数据表情况分析",json3,3l,infos,"28");
        //数据总量情况统计分析
        String json4 = getJson(si);
        getaLong(1l,"数据总量情况统计分析",json4,4l,infos,"28");
        //数据接入数分析
        String json5=getJson(two);
        getaLong(1l,"数据接入数分析",json5,4l,infos,"28");
        //数据接入量分析
        String json6=getJson(two);
        getaLong(1l,"数据接入量分析",json6,4l,infos,"28");
        install(infos);
    }

    @GetMapping("/addDsjpt")
    public void addDsjpt(){
        List<TRegionInfo> tRegionInfos = tRegionInfoMapper.selectTRegionInfo(1l);
        List<TIndicatorsInfo> infos = new ArrayList<>();
        String[] one={"实有人口","网络信息","重点人群","管控人群","特殊人群"};
        String[] two={"2018","2019","2020","2021","2022","2023"};
        String[] two2={"公共设施","交通设施","其他设施","园林绿化设施"};
        String[] san={"工业交通仓储","住宅","商业金融信息","文化娱乐体育"};
        String[] si={"人行通信息","场所位置信息","客运港口、码头信息","公共图书馆地理信息","旅行社信息","酒店信息","民政标准地名地址库地址信息","博物馆信息","游客轨迹信息","窑井(点)信息"};
        String[] wu={"10/12","10/13","10/14","10/15","10/16","10/17","10/18"};
        String[] liu={"法人登记信息","纳税参保缴费","生产经营","司法信息","其他"};
        String[] qi={"检察院","法院","公安","司法"};

         //人口基础库
        String json1 = getJson(one);
        getaLong(1l, "人口基础库", json1, 1l, infos, "29");
        //城市部件库
        String json2=getGroup2(two,two2);
        getaLong(11l,"城市部件库",json2,2l,infos,"29");
        //房屋基础库
        String json3 = getJson(san);
        getaLong(1l,"房屋基础库",json3,3l,infos,"29");
        //地理信息库
        String json4 = getJson(si);
        getaLong(1l,"地理信息库",json4,4l,infos,"29");
        //法人基础库
        String json5=getJson(wu);
        getaLong(1l,"法人基础库",json5,5l,infos,"29");
        //单位基础库
        String json6=getJson(liu);
        getaLong(1l,"单位基础库",json6,6l,infos,"29");
        install(infos);
        //涉案人员基础库
        String json7=getJson(qi);
        getaLong(1l,"涉案人员基础库",json7,7l,infos,"29");
        //案件信息基础库
        String json8=getJson(qi);
        getaLong(1l,"案件信息基础库",json8,8l,infos,"29");
        //涉案财物基础库
        String json9=getJson(qi);
        getaLong(1l,"涉案财物基础库",json9,9l,infos,"29");
        //事件基础库
        String json10=getJson(qi);
        getaLong(1l,"事件基础库",json10,10l,infos,"29");
        //机构基础库
        String json11=getGroup2(two,qi);
        getaLong(1l,"机构基础库",json11,11l,infos,"29");
        //法律文书基础库
        String json12=getJson(qi);
        getaLong(1l,"法律文书基础库",json12,12l,infos,"29");
        install(infos);
    }

    @GetMapping("/updateSafe")
    public void updateSafe(){
        List<TIndicatorsInfo> list= tIndicatorsInfoMapper.selectBylevel1();
        List<TIndicatorsInfo> collect = list.stream().map(item -> {
            String json = getJson();
            item.setCIndicatorsData(json);
            return item;
        }).collect(Collectors.toList());
        for (TIndicatorsInfo tIndicatorsInfo : collect) {
            tIndicatorsInfoMapper.updateTIndicatorsInfo(tIndicatorsInfo);
        }
    }

    private static String getJson() {
        Random rand = new Random();
        String json = String.format(
                "[{\"name\":\"一月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"二月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"三月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"四月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"五月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"六月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"七月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"八月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"九月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"十月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"十一月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}\n" +
                        ",{\"name\":\"十二月\",\"value\":\"%d\",\"max\":\"\",\"group_name\":\"\"}]",
                rand.nextInt(20) + 80,
                rand.nextInt(20) + 80,
                rand.nextInt(20) + 80,
                rand.nextInt(20) + 80,
                rand.nextInt(20) + 80,
                rand.nextInt(20) + 80,
                rand.nextInt(20) + 80,
                rand.nextInt(20) + 80,
                rand.nextInt(20) + 80,
                rand.nextInt(20) + 80,
                rand.nextInt(20) + 80,
                rand.nextInt(20) + 80);
        return json;
    }

    @GetMapping("/updateSaf")
    @Transactional
    public void updateSaf (){
        List<TIndicatorsInfo>  infos=tIndicatorsInfoMapper.selectbyCmoduleId();
        List<TIndicatorsInfo> collect = infos.stream().map(item -> {
            Random rand = new Random();
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                JsonNode jsonArray = objectMapper.readTree(item.getCIndicatorsData());
                // 遍历数组并更新"value"值
                for (JsonNode jsonNode : jsonArray) {
                    // 获取当前对象的"value"字段
                    JsonNode valueNode = jsonNode.get("value");
                    // 检查"value"字段是否存在，并更新其值
                    if (valueNode != null) {
                        int newValue = rand.nextInt(90) + 10;
                        ((ObjectNode) jsonNode).put("value", newValue);
                    }
                }
                item.setCIndicatorsData(jsonArray.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return item;
        }).collect(Collectors.toList());
        for (TIndicatorsInfo tIndicatorsInfo : collect) {
            tIndicatorsInfoMapper.updateTIndicatorsInfo(tIndicatorsInfo);
        }
        }
        @GetMapping("/safenessUpdate")
    public void safenessUpdate() throws JsonProcessingException {
            List<TRegionInfo> tRegionInfos = tRegionInfoMapper.selectTRegionInfoList(null);
            //地区名和行政编码
            Map<String, String> map = new HashMap<>();
            //地区名和经度
            Map<String, String> longitude = new HashMap<>();
            //地区名和纬度
            Map<String, String> latitude = new HashMap<>();
            //地区id 和地区名
            Map<String,String> idAndName=new HashMap<>();
            //地区id 和行政级别
            Map<String,String > idAndLeve =new HashMap<>();
            for (TRegionInfo tRegionInfo : tRegionInfos) {
                map.put(tRegionInfo.getCRegionName(), tRegionInfo.getCAreaCode());
                longitude.put(tRegionInfo.getCRegionName(), tRegionInfo.getCLongitude());
                latitude.put(tRegionInfo.getCRegionName(), tRegionInfo.getCLatitude());
                idAndName.put(String.valueOf(tRegionInfo.getCRegionId()),tRegionInfo.getCRegionName());
                idAndLeve.put(String.valueOf(tRegionInfo.getCRegionId()),tRegionInfo.getCRegionDescribe());
            }
            //获取平安指数的预警地点指标
            QueryWrapper<TIndicatorsInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(TIndicatorsInfo::getCIsRendering, 1)
                    .eq(TIndicatorsInfo::getCModuleId,8)
                    .like(TIndicatorsInfo::getCIndicatorsName,"预警地点");
            List<TIndicatorsInfo> tIndicatorsInfos = indicatorsInfoService.list(queryWrapper);

            List<TIndicatorsInfo> infos = new ArrayList<>();

            for (TIndicatorsInfo tIndicatorsInfo : tIndicatorsInfos) {
                TIndicatorsInfo indicatorsInfo = new TIndicatorsInfo();
                //获取指标数据json转化为实体类
                String data = tIndicatorsInfo.getCIndicatorsData();
                List<RenderingData> renderingData = JSON.parseArray(data, RenderingData.class);
                List<PointData> lists = new ArrayList<>();
                //遍历指标数据设置上图数据
                PointDataVO pointDataVO=new PointDataVO();
                String name = idAndName.get(tIndicatorsInfo.getCDistrictId());
                pointDataVO.setAreaCode(map.get(name));
                pointDataVO.setAreaLevel(idAndLeve.get(tIndicatorsInfo.getCDistrictId()));

                for (RenderingData bean : renderingData) {
                    List<String> value = new ArrayList<>();
                    value.add(longitude.get(bean.getName()));
                    value.add(latitude.get(bean.getName()));
                    PointData pointData=new PointData();
                    pointData.setName(bean.getName());
                    pointData.setScore(bean.getValue());
                    pointData.setCoord(value);
                    lists.add(pointData);
                }
                pointDataVO.setMarkPointData(lists);
                BeanUtils.copyProperties(tIndicatorsInfo, indicatorsInfo);
                if (CollectionUtils.isEmpty(lists)) {
                    indicatorsInfo.setCIsRendering(0L);
                    indicatorsInfo.setCRenderingData("");
                } else {
                    // 使用 Jackson 的 ObjectMapper 将对象转换为 JSON 字符串
                    ObjectMapper objectMapper = new ObjectMapper();
                    String json = objectMapper.writeValueAsString(pointDataVO);
                    indicatorsInfo.setCRenderingData(json);
                }
                List<PointData> collect = pointDataVO.getMarkPointData().stream().collect(Collectors.toMap(PointData::getName, user -> user, (existing, replacement) -> existing))
                        .values().stream().collect(Collectors.toList());
                pointDataVO.setMarkPointData(collect);

                ObjectMapper objectMapper = new ObjectMapper();
                String string = objectMapper.writeValueAsString(pointDataVO);
                BeanUtils.copyProperties(tIndicatorsInfo, indicatorsInfo);
                indicatorsInfo.setCIndicatorsData(string);
                infos.add(indicatorsInfo);
            }
            for (TIndicatorsInfo info : infos) {
                tIndicatorsInfoMapper.updateRenderingData(info);
            }
        }
        @GetMapping("/addSafenes")
        public void addSafeness(){
            List<TRegionInfo> tRegionInfos = tRegionInfoMapper.selectTRegionInfoList(null);
            //地区名和行政编码
            Map<String, String> map = new HashMap<>();
            //地区名和经度
            Map<String, String> longitude = new HashMap<>();
            //地区名和纬度
            Map<String, String> latitude = new HashMap<>();
            //地区id 和地区名
            Map<String,String> idAndName=new HashMap<>();
            //地区id 和行政级别
            Map<String,String > idAndLeve =new HashMap<>();
            for (TRegionInfo tRegionInfo : tRegionInfos) {
                map.put(tRegionInfo.getCRegionName(), tRegionInfo.getCAreaCode());
                longitude.put(tRegionInfo.getCRegionName(), tRegionInfo.getCLongitude());
                latitude.put(tRegionInfo.getCRegionName(), tRegionInfo.getCLatitude());
                idAndName.put(String.valueOf(tRegionInfo.getCRegionId()),tRegionInfo.getCRegionName());
                idAndLeve.put(String.valueOf(tRegionInfo.getCRegionId()),tRegionInfo.getCRegionDescribe());
            }
            //获取平安指数的预警地点指标
            QueryWrapper<TIndicatorsInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(TIndicatorsInfo::getCModuleId,8)
                    .like(TIndicatorsInfo::getCIndicatorsName,"预警地点");
            List<TIndicatorsInfo> tIndicatorsInfos = indicatorsInfoService.list(queryWrapper);
            Random rand = new Random();
            //遍历预警地点指标并重新赋值
            List<TIndicatorsInfo> collect = tIndicatorsInfos.stream().map(item -> {
                PointDataVO pointDataVO = new PointDataVO();
                String name = idAndName.get(item.getCDistrictId());
                pointDataVO.setAreaCode(map.get(name));
                pointDataVO.setAreaName(idAndName.get(item.getCDistrictId()).equals("全国")?"china":idAndName.get(item.getCDistrictId()));
                pointDataVO.setAreaLevel(idAndLeve.get(item.getCDistrictId()));
                List<PointData> vo = new ArrayList<>();
                List<TRegionInfo> tRegionInfos1 = tRegionInfoMapper.selectTRegionInfo(Long.valueOf(item.getCDistrictId()));
                //只取随机的四个地区
                if (tRegionInfos1.size()>4){
                    for (int i = 0; i < 4; i++) {
                        Random random1 = new Random();
                        int randomIndex = random1.nextInt(tRegionInfos1.size());
                        PointData pointData = new PointData();
                        pointData.setName(tRegionInfos1.get(randomIndex).getCRegionName());
                        pointData.setScore(String.valueOf(rand.nextInt(20) + 80));
                        List<String> coord = new ArrayList<>();
                        coord.add(tRegionInfos1.get(randomIndex).getCLongitude());
                        coord.add(tRegionInfos1.get(randomIndex).getCLatitude());
                        pointData.setCoord(coord);
                        vo.add(pointData);
                    }
                }
                //排序
                List<PointData> dataList = vo.stream()
                        .sorted(Comparator.comparing(PointData::getScore).reversed())
                        .collect(Collectors.toList());
                //去重
                List<PointData> distinctPointDataList = new ArrayList<>(dataList.stream()
                        .collect(Collectors.toMap(PointData::getName, Function.identity(), (existing, replacement) -> existing))
                        .values());
                //重新赋值
                pointDataVO.setMarkPointData(distinctPointDataList);
                item.setCIndicatorsData(JSON.toJSONString(pointDataVO));
                return item;
            }).collect(Collectors.toList());
            for (TIndicatorsInfo tIndicatorsInfo : collect) {
                tIndicatorsInfoMapper.updateRenderingData(tIndicatorsInfo);
            }
        }
        @GetMapping("addSafenesYJ")
        public void addSafenesYJ(){
            QueryWrapper<TIndicatorsInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(TIndicatorsInfo::getCModuleId,8)
                    .like(TIndicatorsInfo::getCIndicatorsName,"预警记录");
            List<TIndicatorsInfo> tIndicatorsInfos = indicatorsInfoService.list(queryWrapper);
            String[] aj={"国际公司贪污案","网络诈骗集团调查","离婚财产纠纷","跨境走私冰毒案","环境犯罪调查：污染水源","未成年人网络欺凌案","医疗保险欺诈调查","公司内部受贿事件","交通肇事致命事故","知识产权侵权纠纷"};
            // 创建一个Random对象
            Random random = new Random();
            TSafenessIndex tSafenessIndex = new TSafenessIndex();
            List<TSafenessIndex> items = safenessIndexService.selectTSafenessIndexList(tSafenessIndex);
            Map<Long,String> maps=new HashMap<>();
            for (TSafenessIndex ite : items) {
                maps.put(ite.getcSafenessId(),ite.getcYear()+"."+ite.getcMonth());
            }
            List<TIndicatorsInfo> collect = tIndicatorsInfos.stream().map(item -> {
                List<KeyValueVO> keyValue=new ArrayList<>();
                // 从数组中随机选择5个元素
                for (int i = 0; i < 5; i++) {
                    Random random1 = new Random();
                    int randomIndex = random1.nextInt(aj.length);
                    KeyValueVO keyValueVO=new KeyValueVO();
                    keyValueVO.setName(maps.get(item.getCSafenessId()) + "-" + aj[randomIndex]);
                    keyValueVO.setValue(String.valueOf(random.nextInt(20) + 80));
                    keyValue.add(keyValueVO);
                }
                List<KeyValueVO> sortedList = keyValue.stream()
                        .sorted(Comparator.comparing(KeyValueVO::getValue).reversed())
                        .collect(Collectors.toList());
                String jsonString = JSON.toJSONString(sortedList);
                item.setCIndicatorsData(jsonString);
                return item;
            }).collect(Collectors.toList());
            for (TIndicatorsInfo tIndicatorsInfo : collect) {
                tIndicatorsInfoMapper.updateRenderingData(tIndicatorsInfo);
            }
        }
    public static void main(String[] args) {
        int numMonths = 395;
        generateRandomMonths(numMonths);
    }
    public static void generateRandomMonths(int numMonths) {
        // 初始日期设定为2023年1月1日
        Calendar calendar = Calendar.getInstance();
        calendar.set(2023, Calendar.JANUARY, 1);

        for (int i = 0; i < numMonths; i++) {
            // 生成1到12之间的随机月份
            int randomMonth = (int) (Math.random() * 12) + 1;

            // 确保两个月份之间的间隔为5个月
            calendar.add(Calendar.MONTH, 5);

            // 设置新的月份
            calendar.set(Calendar.MONTH, randomMonth);

            // 获取年份
            int year = calendar.get(Calendar.YEAR);

            // 获取月份，并在一位数的情况下添加0
            String month = String.format("%02d", calendar.get(Calendar.MONTH) + 1);

            // 打印结果
            System.out.println(year + "-" + month);
        }
    }
    }


